const implAddress = getApp().globalData.implAddress;
Page({
  buttonStatus: false,
  data: {
    orderContent: {
      order_id: 1,
      item_img: '/images/indexIcon/testPoster/poster1.png',
      item_name: '第一个示例',
      order_time: '2024年2月22日 22:22',
      order_account: 100,
      status: '排队中'
    }
  },
  onLoad: async function (options) {
    const that = this
    //获取id并查询
    const orderId = options.orderId
    //查询订单
    var order;
    async function fetchOrder(orderId) {
      return new Promise((resolve, reject) => {
        wx.request({
          url: `${implAddress}order/orderContent/${orderId}`,
          method: 'GET',
          success: (res) => {
            if (res.statusCode === 200) {
              let resOrder = res.data.data;
              resOrder.forEach(item => {
                switch (item.status) {
                  case "0":
                    item.status = '排队中';
                    break;
                  case "1":
                    item.status = '派单中';
                    break;
                  case "2":
                    item.status = '已完成';
                    break;
                  default:
                    item.status = '未知状态';
                }
              });
              resolve(resOrder[0]); // 假设我们只对第一个订单感兴趣  
            } else {
              reject(new Error('请求失败: ' + res.statusCode));
            }
          },
          fail: reject
        });
      });
    }
    try {
      order = await fetchOrder(orderId);
    } catch (error) {
      console.error('获取订单信息失败:', error);
    }
    //查询订单对应商品
    let item;
    async function fetchItemId() {
      return new Promise((resolve, reject) => {
        wx.request({
          url: `${implAddress}item/${order.item_id}`,
          method: 'GET',
          success: (res) => {
            if (res.statusCode === 200) {
              let resItem = res.data.data;
              resolve(resItem[0]);
            } else if (res.data.code === 0) {
              wx.navigateBack()
              wx.showToast({
                title: '订单错误',
                icon: 'error'
              })
            } else {
              reject(new Error('请求失败: ' + res.statusCode));
            }
          },
          fail: reject
        });
      });
    }
    try {
      item = await fetchItemId();
    } catch (error) {
      console.error('获取订单信息失败:', error);
    }
    //拼凑信息
    const preOrderContent = {
      order_id: order.order_id,
      item_img: item.item_img,
      item_name: item.item_name,
      order_time: this.formatTimestamp(parseInt(order.order_time) * 1000),
      order_account: order.order_account,
      status: order.status
    }
    //设置data中的内容
    that.setData({
      orderContent: preOrderContent
    })
  },

  //转换时间戳
  formatTimestamp(timestamp) {
    // 创建一个Date对象  
    const date = new Date(timestamp);

    // 获取年月日时分秒  
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的  
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    // 拼接成字符串  
    return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
  },

  //评价
  comment(status) {
    const that = this
    wx.showToast({
      title: '感谢您的反馈！',
      icon: 'success'
    })
    that.setData({
      buttonStatus: !this.buttonStatus
    })
  }
})